import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/Index'
import store from '../store';
Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    redirect:'/index'
    // component: Index
  },
  {
    path: '/Index',
    name: 'Index',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Index.vue')
  },
    {
      path: '/Other/:id/:learningNum',
      name: 'Other',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/Other.vue')
    },
    {
      path: '/BuyCar',
      name: 'BuyCar',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/BuyCar'),
      meta: {
        needLogin: true
      }
    },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  // console.log(to)
  // console.log(from)
  // 判断这个界面是否需要登录后访问
  if (to.meta.needLogin) {
    // console.log("88888888888888")
    if (store.state.isLogin) {
      next()
    }else {
      next("/",)
      store.commit("alertBuyCar", {flag:true})
    }

  } else {
    next();
  }
})

export default router
